package com.warm.common.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.io.*;

/**
 * @Description 读取json文件
 * @Author warmchuang
 * @Date 2021/2/23 14:10
 */
public class JSONUtil {

    public static JSONObject parseJSONObject(String fileName) {
        File jsonFile = new File(fileName);
        return parseJSONObject(jsonFile);
    }

    public static JSONObject parseJSONObject(File file){
        try {
            FileReader fileReader = new FileReader(file);
            Reader reader = new InputStreamReader(new FileInputStream(file),"utf-8");
            int ch = 0;
            StringBuffer sb = new StringBuffer();
            while ((ch = reader.read()) != -1) {
                sb.append((char) ch);
            }
            fileReader.close();
            reader.close();
            return JSON.parseObject(sb.toString());
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}
